<?php
namespace app\manage\controller;

use think\Request;

/**
* 菜单管理
* @author xuyunlong <416319808@qq.com>
*/
class Sys extends Common
{
	private $menu;

	public function _initialize()
    {
        $this->menu = model('menu');
    }


    /**
	 * 菜单添加编辑
	 */
	public function menu()
	{
		if (Request::instance()->isPost()) {
			$data = input('post.');
			$validate = validate('Menu');

			if (!$data['id']) {
				unset($data['id']);

				if($validate->scene('add')->check($data)){
					if (!$this->menu->data($data)->save()) {
						return json(['status' => 0, 'info' => '保存失败']);
					} else {
						return json(['status' => $this->menu->id, 'info' => '保存成功']);
					}
				} else {
					return json(['status' => 0, 'info' => $validate->getError()]);
				}
			} else {
				if($validate->scene('edit')->check($data)){

					if (!$this->menu->save($data, $data['id'])) {
						return json(['status' => 0, 'info' => '保存失败']);
					} else {
						return json(['status' => $this->menu->id, 'info' => '保存成功']);
					}
				} else {
					return json(['status' => 0, 'info' => $validate->getError()]);
				}
			}
		} else {
			return view('',['title'=>'菜单编辑']);
		}
	}


	public function parentNode()
	{
		$data = $this->menu->node();
		exit(json_encode($data));
	}

	/**
	 * 是否禁用
	 * @return [type] [description]
	 */
	public function mdis(){
		if(request()->isAjax()){
			//批量更新
			$mid = input("post.mid/a");
			$type = input("post.type");
			$save = array();
			foreach ($mid as $key=>$value) {
				$save[$key]['id'] = $value;
				$save[$key]['isdis'] = $type;
			}
			$this->menu->saveAll($save);
			return true;
		}
	}




}
